import React from 'react';
import {injectIntl} from './intl';
import PropTypes from 'prop-types';
import {Button} from 'antd';
import ExcelImport from '../common/ExcelImport';
import routes from './routes';
import fileTemplate from '../attachment/经销商信息批量导入模板.xlsx';
const basePath = fetch.basePath || '';

class FileImport extends React.PureComponent {
    constructor(props) {
        super(props);
        this.state = {
            loading: false,
            success: false
        };
    }

    beforeUpload = () => this.setState({loading: true});

    afterUpload = ({ok}) => this.setState({
        loading: false,
        success: this.state.success || ok
    });

    onReturn = () => this.props.onReturn(this.state.success);

    render() {
        const description = [
            this.props.intl.formatMessage({
                id: 'fileImport.step2.description1',
                defaultMessage: '所属总代编号、经销商编号、经销商名称不能为空；'
            }),
            this.props.intl.formatMessage({
                id: 'fileImport.step2.description3',
                defaultMessage: '总代编号 不可重复；'
            })
        ];
        return ([
            <div key="return" className="page-toolbar">
                <Button type="primary" onClick={this.onReturn} disabled={this.state.loading}>
                    {this.props.intl.formatMessage({
                        id: 'fileImport.btm.return',
                        defaultMessage: '返回'
                    })}
                </Button>
            </div>,
            <ExcelImport
                key="import"
                template={fileTemplate}
                action={`${basePath}/parts/api/v1/subDealers/import`}
                description={description}
                beforeUpload={this.beforeUpload}
                afterUpload={this.afterUpload}/>
        ]);
    }
}

FileImport.propTypes = {
    intl: PropTypes.object,
    onReturn: PropTypes.func
};

import {connect} from 'react-redux';
import {querySearch} from './actions';

const mapDispatchToProps = (dispatch, ownProps) => ({
    onReturn: success => {
        if(success)
            dispatch(querySearch());
        ownProps.history.push(routes.query.url());
    }
});

export default connect(
    null,
    mapDispatchToProps
)(injectIntl(FileImport));
